package br.edu.sistemaacademico.builder;



import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.pojo.Classe;
import br.edu.sistemaacademico.pojo.Curso;
import br.edu.sistemaacademico.pojo.Disciplina;

public class ClasseDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "curso", "disciplina", "periodo", "status" });

	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Classe c = (Classe) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getCurso().getId(), c.getDisicplina().getId(), c.getPeriodo(), c.getStatus() });

		String condicao = "codigo = " + c.getId() + " and status=1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_CLASSE");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Object> getAll( String condicao, String tabelasAdicionais, String ordenacao){
		List<Classe> list = new LinkedList<Classe>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Classe a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.colunasToReturn = "a.*";
		select.condicao = condicao+" and status =1";
		select.orderBy = ordenacao;
		
		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next())
				list.add((Classe) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Classe c = new Classe();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Classe a "+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.setCondicao(condicao+" and status =1");
		select.colunasToReturn = "a.*";
	//	select.setCondicao("status =1");

		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				c = (Classe) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Classe c = (Classe) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getCurso().getId(), c.getDisicplina().getId(), c.getPeriodo(), 1 });

		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_Classe");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Classe a = new Classe();
		CursoDAO cursoDao = new CursoDAO();
		DisciplinaDAO disciplinaDAO = new DisciplinaDAO();
		Long id;
		a.setCurso((Curso)cursoDao.getObject(" codigo="+rs.getLong(COLUNAS.get(1)), ""));
		a.setDisicplina((Disciplina) disciplinaDAO.getObject("codigo= "+rs.getLong(COLUNAS.get(2)),""));
		a.setId(rs.getLong(COLUNAS.get(0)));
		id = rs.getLong(COLUNAS.get(2));
		if(id==null)
			a.getDisicplina().setId(0l);
		else
		a.setPeriodo(rs.getInt(COLUNAS.get(3)));
		a.setStatus(rs.getInt(COLUNAS.get(4)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Classe c = (Classe) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getCurso().getId(), c.getDisicplina().getId(), c.getPeriodo() });;

		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Classe");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void deletar(Object o) {
		Classe c = (Classe) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getCurso().getId(), c.getDisicplina().getId(), c.getPeriodo(), 0 });

		String condicao = "codigo = " + c.getId() + " and status=1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_CLASSE");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
